package android.homeaccount;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.homeaccount.helper.DataGetter;
import android.homeaccount.helper.Services;
import android.homeaccount.models.M_Income;
import android.homeaccount.packages.P_IncomeData;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DetailIncome extends Activity
{
	ListView lvDetailIncome;
	
	DataGetter dg;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailincome);
        
        dg = new DataGetter(DetailIncome.this);
        
        lvDetailIncome = (ListView) findViewById(R.id.lvDetailIncome);
        
        if(isOnline())
			new IncomeData().execute();
		else
			AlertNoNetwork(this);
    }
    
    private class IncomeData extends AsyncTask<String, Void, P_IncomeData>
	{
		private final ProgressDialog dialog = new ProgressDialog(DetailIncome.this);

		protected void onPreExecute()
	    {
			this.dialog.setMessage(getString(R.string.s_working));
			this.dialog.show();
	    }

		protected P_IncomeData doInBackground(final String... args)
		{
			Services s = new Services();
			P_IncomeData id = s.Income(dg.GetToken());
			return id;
		}

		protected void onPostExecute(final P_IncomeData id)
		{
			if(id != null)
			{
				if(id.Income!= null)
				{
					lvDetailIncome.setAdapter(new IncomeAdapter(DetailIncome.this, id.Income));
					
					if (this.dialog.isShowing())
					{
						this.dialog.dismiss();
					}
	    	          
					lvDetailIncome.setOnItemClickListener(new OnItemClickListener()
					{
						@Override
						public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id)
						{
//							Intent i = new Intent(SearchProvince.this, SearchDistrict.class);
//							Bundle b = new Bundle();
//							b.putStringArray("district_id", dis_id);
//							b.putStringArray("district", dis);
//							i.putExtras(b);
//							startActivityForResult(i, 0);
						}
					});
				}
				else
				{  
					AlertCannotGetData(DetailIncome.this);
				}
			}
			else
			{
				AlertCannotGetData(DetailIncome.this);
			}  
	    	  
			if (this.dialog.isShowing())
			{
				this.dialog.dismiss();
			}
		}
	}
    
    private class IncomeAdapter extends BaseAdapter
    {     
        private Activity activity;
        private M_Income[] data;
        private LayoutInflater inflater = null;
        
        public IncomeAdapter(Activity a, M_Income[] i)
        {
            activity = a;
            data = i;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount()
        {
            return data.length;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }
        
        public class ViewHolder
        {
        	public TextView tvDate;
            public TextView tvDetail;
            public TextView tvIncome;
            public TextView tvExpend;
            public TextView tvBalance;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            View vi = convertView;
            ViewHolder holder;
            if(convertView == null)
            {
                vi = inflater.inflate(R.layout.item_income, null);
                holder = new ViewHolder();
                holder.tvDate = (TextView)vi.findViewById(R.id.tvDate);
                holder.tvDetail = (TextView)vi.findViewById(R.id.tvDetail);
                holder.tvIncome = (TextView)vi.findViewById(R.id.tvIncome);
                holder.tvExpend = (TextView)vi.findViewById(R.id.tvExpend);
                holder.tvBalance = (TextView)vi.findViewById(R.id.tvBalance);
                vi.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)vi.getTag();
            }
            
            holder.tvDate.setText((data[position].date != null?data[position].date : " - "));
            holder.tvDetail.setText((data[position].detail != null?data[position].detail : " - "));
            holder.tvIncome.setText((data[position].income != null?data[position].income : " - "));
            holder.tvExpend.setText((data[position].expend != null?data[position].expend : " - "));
            holder.tvBalance.setText((data[position].balance != null?data[position].balance : " - "));
            
            return vi;
        }
    }
    
    private Boolean isOnline()
	{
		ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
				|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)
			return true;		
		else return false;
	}
    
    public void AlertNoNetwork(Context c)
	{
		AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
		  alertbox.setMessage(getString(R.string.s_no_network));
		  alertbox.setNeutralButton(getString(R.string.s_ok),null);
		  alertbox.show();		
	}
    
    public void AlertCannotGetData(Context c)
	{
		AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
		  alertbox.setMessage(getString(R.string.s_not_get_data));
		  alertbox.setNeutralButton(getString(R.string.s_ok),null);
		  alertbox.show();		
	}
}